﻿package bdz.projects.slab.managers {
	import bdz.projects.slab.core.SlabSection;
	import bdz.projects.slab.events.SlabSiteEvent;
	import bdz.common.utils.BitmapUtils;
	import flash.display.DisplayObject;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.utils.*;
	import flash.events.*;
	
	/**
	* ...
	* @author Default
	*/
	public class SlabSectionManager extends Sprite {
		
		private var currentSection:SlabSection;
		private var _preloaderObj:Object;
		private var _bg:MovieClip;
		private var _preloader:ContentPreloader;
		function SlabSectionManager():void {
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		private function initializeComponent(evt:Event):void {
			_bg = new MovieClip();
			BitmapUtils.createBounds(_bg, 688, 624, 0);
			_bg.cacheAsBitmap = true;
			addChild(_bg);
		}
		private function preloaderTransitionOut(evt:SlabSiteEvent):void {
			try{
				removeChild(evt.target as DisplayObject);
			}catch (e:Error) {
				
			}
		}
		public function addSection(cls:Object, props:Object = null):* {
			try {
				removeChild(_preloader);
			}catch (e:Error) {
				
			}
			var sectObj:Object = getDefinitionByName(getQualifiedClassName(cls));
			var sect:SlabSection = new sectObj() as SlabSection;
			if (props) {
				for(var prop:String in props){
					sect[prop] = props[prop];
				}
			}

			sect.addEventListener(SlabSiteEvent.SECTION_TRANSITIONOUT, preloaderTransitionOut);
			if (currentSection) {
				currentSection.transitionOut();
			}
			addChild(sect);
			currentSection = sect;
			return sect;
		}
		private function createPreloader():void {
			try {
				removeChild(_preloader);
			}catch (e:Error) {
				
			}
			_preloader = new ContentPreloader();
			_preloader.x = 0;
			_preloader.y = 180;
			addChild(_preloader);
		}
		public function showPreloader():void {
			if(currentSection){
				currentSection.transitionOut();
			}
			createPreloader();
			
		}
		public function setPreloaderClass(obj:Object):void {
			_preloaderObj = obj;
		}
	}
	
}